// 业务逻辑中间件
const path = require('path')
const {
    getFileData
} = require('../utils/file_utils')
module.exports = async (ctx, next) => {
    let url = ctx.request.url
    let filePath = url.replace('/api', '')
    filePath = '../data' + filePath + '.json'
    filePath = path.join(__dirname, filePath)

    let data = {}

    try {
        data = await getFileData(filePath)
    } catch (error) {
        data = {
            message: '数据获取失败，路径不存在',
            status: 404
        }
    }
    
    ctx.response.body = data
    await next()

}